package com.example.schoolhospitalsystem;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private EditText etId, etPassword;
    private Button loginButton;
    private Spinner spRole;
    private String selectedRole;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etId = findViewById(R.id.et_id);
        etPassword = findViewById(R.id.et_password);
        loginButton = findViewById(R.id.btn_login);
        spRole = findViewById(R.id.sp_role);

        // 初始化角色选择器
        initRoleSpinner();

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = etId.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                if (id.isEmpty() || password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请输入学号/工号和密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 模拟登录验证
                if (validateLogin(id, password, selectedRole)) {
                    // 登录成功，跳转到主界面
                    Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                    intent.putExtra("userId", id);
                    intent.putExtra("userRole", selectedRole);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "登录失败，学号/工号、密码或身份错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initRoleSpinner() {
        List<String> roleList = new ArrayList<>();
        roleList.add("学生");
        roleList.add("老师");
        roleList.add("医生");
        roleList.add("管理医生");

        ArrayAdapter<String> roleAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_item, roleList);
        roleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spRole.setAdapter(roleAdapter);

        spRole.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedRole = (String) parent.getItemAtPosition(position);
                if (selectedRole.equals("学生")) {
                    selectedRole = "student";
                } else if (selectedRole.equals("老师")) {
                    selectedRole = "teacher";
                } else if (selectedRole.equals("医生")) {
                    selectedRole = "doctor";
                } else if (selectedRole.equals("管理医生")) {
                    selectedRole = "admin_doctor";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private boolean validateLogin(String id, String password, String role) {
        // 这里应该连接数据库进行验证
        // 简化示例，添加更多测试账号
        if (role.equals("student")) {
            return id.equals("001") && password.equals("001");
        } else if (role.equals("teacher")) {
            return id.equals("001") && password.equals("001");
        } else if (role.equals("doctor")) {
            return id.equals("001") && password.equals("001");
        } else if (role.equals("admin_doctor")) {
            return id.equals("001") && password.equals("ad001");
        }
        return false;
    }
}